<?php
/**
 * 管理员
 * Created by Vscode.
 * User: Luanzg
 * Date: 2020/05/22 1526
 * Time: 15:26
 */
namespace app\common\validate;

use app\common\facade\model\AdminModel;

class Admin extends BaseValidate
{

    /**
     * 姓名唯一
     * 自定义验证器
     */
    protected function nameUnique($value, $rule, $data)
    {

        $where[] = ['username', '=', $value];

        $info = AdminModel::info($where, ['id']);

        if (!empty($info)) exception('姓名已存在');

        return true;
    }

    protected $field = [
        'username'      => '登录账号',
        'password'  => '密码',
        'password2' => '确认密码',
        'enable'    => '启用',
        'roleId'    => '角色',
        'id'        => 'ID',
        'mobile'    => '手机号',
        'email'     => '邮箱',
        'depart'    => '部门',
        'channel'   => '频道'
    ];

    protected $rule = [
        'username'       => 'require|max:20|nameUnique',
        'password'   => 'require|alphaDash|min:6',
        'password2'  => 'requireWith:password|confirm:password',
        'enable'     => 'require|in:2,3',
        'roleId'     => 'require|number|max:10',
        'id'     => 'require|number|max:10',
        'mobile' => 'require|number|max:11',
        'email'  => 'email',
        'depart' => 'require',
        'channel'=> 'require'
    ];

    protected $message = [
        'password2.confirm' => "确认密码和密码不一致",
        'mobile' => "手机号格式错误",
        'email'  => "邮箱格式错误"
    ];

    /**
     * 登录
     */
    protected function sceneLogin()
    {
        return $this->only([
            'username',
            'password'
        ])->remove([
            'username'     => 'nameUnique',
            'password' => 'min',
        ]);
    }

    /**
     * 添加【保存】
     */
    protected function sceneAddSave()
    {
        return $this->only([
            'uesrname',
            'depart',
            'channel'
        ]);
    }

    /**
     * 编辑【保存】
     */
    protected function sceneEditSave()
    {
        return $this->only([
            'depart',
            'channel'
        ]);
    }

    /**
     * 编辑个人信息【保存】
     */
    protected function sceneSelfSave()
    {
        return $this->only([
            'truename',
            'mobile',
            'email'
        ]);
    }

    /**
     * 修改密码【保存】
     */
    protected function scenePasswordEditSave()
    {
        return $this->only([
            'password',
            'password2',
        ]);
    }




}
